package com.bite.spring.demo.sample;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2025-08-28
 * Time: 21:03
 */
@RequestMapping("/Request")
@RestController
public class RequestController {

    @RequestMapping("/r1")
    public String r1(String keyword){
        return "接收参数：" + keyword;
    }

    @RequestMapping("/r2")
    public String r2(String userName, String passWord){
        return "接收参数：userName：" + userName + "，passWord：" + passWord;
    }

    @RequestMapping("/r3")
    public String r3(Integer number){
        return "接收参数：" + number;
    }

    @RequestMapping("/r4")
    public String r4(int number){
        return "接收参数：" + number;
    }

    @RequestMapping("/r5")
    public UserInfo r5(UserInfo userInfo){
        return userInfo;
    }

    //从前端接收参数q，赋值给keyword
    @RequestMapping("/r6")
    public String r6(@RequestParam(value = "q",required = false)String keyword){
        return "接收参数=" + keyword;
    }

    //传递数组
    @RequestMapping("/r7")
    public String r7(String[] arr){
        return "接收的参数：" + Arrays.toString(arr);
    }

    //传递集合
    @RequestMapping("/r8")
    public String r8(@RequestParam List<Integer> list){
        return "接收参数：list=" + list;
    }

    //传递json对象
    @RequestMapping("/r9")
    public String r9(@RequestBody UserInfo userInfo){
        return userInfo.toString();
    }

    //从url中获取参数
    //从中匹配到对应的articleId，然后就赋值到r10中
    @RequestMapping("/article/{articleId}")
    //路径变量PathVariable，只要符合这种形式，就把上面的id赋值到下面的id
    public String r10(@PathVariable  Integer articleId){
        return "获取文章ID：" + articleId;
    }

    @RequestMapping("/article/{type}/{articleId}")
    //收到的结果都是String类型，根据String是否能转成Integer来进行的，能转就ok，不能就报错
    public String r11(@PathVariable  Integer articleId, @PathVariable("type") String articletype ){
        return "获取文章ID：" + articleId + ",type:" + articletype;
    }

    //上传文件
    @RequestMapping("/r12")
    //重命名 RequestPart
    public String r12(@RequestPart("file11") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        //文件上传
        file.transferTo(new File("D:\\tmp\\" + file.getOriginalFilename()));
        return "文件获取成功";
    }

    @RequestMapping("/r13")
    //两个必须要理解的参数
    public String r13(HttpServletRequest request, HttpServletResponse response){
        //request.getCookie.var 获取数组
        Cookie[] cookies = request.getCookies();

        if(cookies != null){
            for(Cookie cookie : cookies){
                System.out.println(cookie.getName() + ": " + cookie.getValue());
            }
        }
        return "返回Cookie成功";
    }

    @RequestMapping("/r14")
    public String r14(@CookieValue("java") String java){
        return "从Cookie中获取Java的值:" + java;
    }

    //存储session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //从cookie中获取sessionId,根据sessionID 获取session关键字
        //request.getSession.var;
        HttpSession session = request.getSession();
        //默认存储在内存中，会随着应用重启而消失
        //登录的用户名称
        session.setAttribute("userName", "zhangsan");
        session.setAttribute("age", 27);
        return "设置session成功";
    }

    //获取session
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        //从cookie中获取sessionId, 根据sessionID 获取session关键字
        //request.getSession.var;
        HttpSession session = request.getSession(false);
        //如果用户登录，session有值，未登录，则session为null
        if (session == null) {
            return "用户未登录";
        } else {
            //从session中获取登录的用户信息
            String userName = (String) session.getAttribute("userName");
            return "登录用户名为： " + userName;
        }
    }

    //获取session
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {
        String userName = (String) session.getAttribute("userName");
        return "登录用户名为： " + userName;
    }

    //获取session
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("userName") String userName) {
        return "登录用户名为： " + userName;
    }

    //获取Header
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "从header中获取userAgent" + userAgent;
    }


    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        return "从header中获取userAgent" + userAgent;
    }
}
